package ${allPackage}${controllerPackage};

import ${allPackage}${servicePackage}.${domainUpper}Service;
import ${allPackage}${domainPackage}.${domainUpper};
import com.canteen.create.result.ResponseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * ${domainDesc}控制层
 * @author 自动生成
 * @description 自动生成
 * @create ${createTime}
 */
@RestController
@RequestMapping(value = "/${domain}")
@Slf4j
@Api(tags = "${domainDesc}")
public class ${domainUpper}Controller {

    private final ${domainUpper}Service ${domain}Service;

    public ${domainUpper}Controller(${domainUpper}Service ${domain}Service) {
        this.${domain}Service = ${domain}Service;
    }

    /**
     * ${domainDesc}分页查询列表
     * @param page 页码
     * @param limit 单页条数
     * @return
     */
    @GetMapping(value = "/list", name = "${domainDesc}分页查询列表")
    @ApiOperation("${domainDesc}分页查询列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        try {
            PageInfo<${domainUpper}> pages = ${domain}Service.getList(page, limit);
            log.info("${domainDesc}分页查询列表成功");
            return ResponseResult.successWrappedResult("${domainDesc}分页查询列表成功", pages.getList(), pages.getTotal());
        }catch (Exception e){
            log.info("${domainDesc}分页查询列表失败："+e.getMessage());
            return ResponseResult.failWrappedResult("${domainDesc}分页查询列表失败："+e.getMessage());
        }
    }

    /**
     * 修改${domainDesc}（新增、修改）
     * @param ${domain}
     * @return
     */
    @PostMapping(value = "/edit", name = "修改${domainDesc}")
    @ApiOperation("修改${domainDesc}（新增、修改）")
    public ResponseResult edit(${domainUpper} ${domain}){
        try {
            if (Objects.isNull(${domain}.get${primaryKey}()) || ${domain}.get${primaryKey}()${judgePrimaryKey}){
                //新增
                ${domain}Service.saveOne(${domain});
                log.info("${domainDesc}新增成功");
            }else {
                //编辑
                ${domain}Service.update(${domain});
                log.info("${domainDesc}修改成功");
            }
            return ResponseResult.successWrappedResult("${domainDesc}编辑成功");
        }catch (Exception e){
            log.info("${domainDesc}编辑失败："+e.getMessage());
            return ResponseResult.failWrappedResult("${domainDesc}编辑失败："+e.getMessage());
        }
    }

    /**
     * 删除${domainDesc}
     * @param primaryKey
     * @return
     */
    @PostMapping(value = "/delete", name = "删除${domainDesc}")
    @ApiOperation("删除${domainDesc}")
    public ResponseResult delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.failWrappedResult("没有找到${domainDesc}编号");
            }
            ${domain}Service.delete(primaryKey);
            log.info("${domainDesc}删除成功");
            return ResponseResult.successWrappedResult("${domainDesc}删除成功");
        }catch (Exception e){
            log.info("${domainDesc}删除失败："+e.getMessage());
            return ResponseResult.failWrappedResult("${domainDesc}删除失败："+e.getMessage());
        }
    }
}